package com.burgeon.backend.SHOPBACKEND.controller;

import com.burgeon.backend.SHOPBACKEND.entity.*;
import com.burgeon.backend.SHOPBACKEND.entity.FAB.FABVo;
import com.burgeon.backend.SHOPBACKEND.entity.FAB.FabEntity;
import com.burgeon.backend.SHOPBACKEND.mappers.AndroidMapper;
import com.burgeon.backend.SHOPBACKEND.service.AWechatService;
import com.burgeon.backend.SHOPBACKEND.service.AndroidService;
import com.burgeon.backend.SHOPBACKEND.service.FABservice;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RequestMapping("App")
@RestController
@CrossOrigin
public class AndroidController extends BaseController {
    @Autowired
    private AndroidService androidService;
    @Autowired
    AWechatService wechatService;
    @Autowired
    private AndroidMapper androidMapper;

    @RequestMapping("uploadImg")
    public ResponseResult<WxChatImage> uploadImg(MultipartFile file, String ip){
        String token = getAccount(ip);
        WxChatImage data = wechatService.upWeChatImage(file, token);
        return new ResponseResult(SUCCESS,data);
    }

    public String getAccount(String url){
        System.err.println("IP穿"+url);
        String token = androidMapper.GetTokenByUrl(url);
        System.err.println(token);
        String account = androidMapper.GetTokenByToken(token);
        System.err.println(account);
        return account;

    }


    /**
     * 生成绑定导购二维码存入服务器 返回图片连接
     *
     * @param ip 环境连接
     * @param id 导购id
     * @return
     */
    @RequestMapping("createQRcode")
    public ResponseResult<String> createQRcode(String ip, String id) {
        String data = androidService.createQRcode(ip, id);
        return new ResponseResult<String>(SUCCESS, data);
    }



    /**
     * 判断是否已生成导购绑定二维码 有返回图片连接
     *
     * @param ip 环境连接
     * @param id 导购id
     * @return
     */
    @RequestMapping("judgeQRcode")
    public ResponseResult<HashMap> judgeQRcode(String ip, String id) {
        HashMap data = androidService.judgeQRcode(ip, id);
        return new ResponseResult<HashMap>(SUCCESS, data);
    }

    /**
     *  App上传头像
     * @param file
     * @param ip
     * @return
     */
    @RequestMapping("VipImg")
    public ResponseResult<Integer> VipImg(MultipartFile file,String ip, String erpVid,String openId,String uCardNo){

        androidService.upVipImg(file,ip,erpVid,openId,uCardNo);
        return new ResponseResult<>(SUCCESS);
    }

    /**
     * 品牌分组
     * @param ip
     * @return
     */
    @RequestMapping("getBrandByIp")
    public ResponseResult<ArrayList<BrandApp>> findBrandByIp(String ip){

        ArrayList<BrandApp> data = androidService.getBrandByIp(ip);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     *搜索品牌分组
     * @param ip
     * @param name
     * @return
     */
    @RequestMapping("findBrandByName")
    public ResponseResult<ArrayList<BrandApp>> findBrandByName(String ip,String name){

        ArrayList<BrandApp> data = androidService.findBrandByName(ip,name);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 获取商品根据小分类id
     * @param brandId
     * @return
     */
    @RequestMapping("GetGoodsLinkByBrandId")
    public ResponseResult<List<LinkGoodsVo>> GetGoodsLinkByBrandId(String brandId, String ip, Integer page,String daoId){

        List<LinkGoodsVo> data = androidService.GetGoodsLinkByBrandId(brandId,ip,page,daoId);

        return new ResponseResult<>(SUCCESS,data);
    }



    /**
     * 商城分组
     * @param ip
     * @return
     */
    @RequestMapping("findClassify")
    public ResponseResult<List<ClassifyVo>> addMaterialGroup(String ip){

        List<ClassifyVo> data = androidService.getClassify(ip);

        return new ResponseResult<>(SUCCESS,data);
    }




    /**
     *
     * @param ip
     * @param name
     * @return
     */
    @RequestMapping("findByName")
    public ResponseResult<List<ClassifyVo>> findByName(String ip,String name){

        List<ClassifyVo> data = androidService.findByName(ip,name);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 获取商品根据小分类id
     * @param cIId
     * @return
     */
    @RequestMapping("findGoods")
    public ResponseResult<List<Goods>> findSGoods(String cIId,Integer start){

        List<Goods> data = androidService.getGoodsAll(cIId,start);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 获取商品根据小分类id
     * @param cIId
     * @return
     */
    @RequestMapping("findGoodsLink")
    public ResponseResult<List<LinkGoodsVo>> findSGoodsLink(String cIId, String ip, Integer start,String daoId){

        List<LinkGoodsVo> data = androidService.getGoodsLink(cIId,ip,start,daoId);

        return new ResponseResult<>(SUCCESS,data);
    }




    /**
     * 根据code
     * @param code
     * @param ip
     * @param start
     * @return
     */
    @RequestMapping("findByCode")
    public ResponseResult<List<LinkGoodsVo>> findByCode(String code, String ip, Integer start,String daoId){

        List<LinkGoodsVo> data = androidService.findByCode(code,start,ip,daoId);

        return new ResponseResult<>(SUCCESS,data);
    }

/**++++++++++++++++++++++++++++++++++++++++++品牌分组++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

    /**
     * 新增素材分组
     * @param file 封面
     * @param groupName 名称
     * @param ip ip
     * @return 修改0或1
     */
    @RequestMapping("addGroup")
    public ResponseResult<Integer> addMaterialGroup(MultipartFile file, String groupName, String ip){
        System.err.println(groupName+ip);
        Integer data = androidService.insertMaterialGroup(file,groupName,ip);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 获取所有分组
     * @param ip
     * @return
     */
    @RequestMapping("getGroupAll")
    public ResponseResult<ArrayList<MaterialGroup>> getGroupAll(String ip){

        ArrayList<MaterialGroup> data = androidService.findGroupByAccount(ip);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 搜索分组
     * @param ip
     * @param groupName
     * @return
     */
    @RequestMapping("findGroupByName")
    public ResponseResult<ArrayList<MaterialGroup>> findGroupByName(String ip,String groupName){

        ArrayList<MaterialGroup> data = androidService.findGroupByName(ip,groupName);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 修改分组
     * @param file
     * @param id
     * @param groupName
     * @param ip
     * @return
     */
    @RequestMapping("upGroup")
    public ResponseResult<Integer> upGroup(MultipartFile file,String id,String groupName, String ip){

        Integer data = androidService.upGroupName(file,id,groupName,ip);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 改变顺序
     * @param beforeId
     * @param afterId
     * @return
     */
    @RequestMapping("upGroupId")
    public ResponseResult<Integer> upGroupId(String beforeId, String afterId){

        Integer data = androidService.upGroupId(beforeId,afterId);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 删除素材分组
     * @param id
     * @param ip
     * @return
     */
    @RequestMapping("deGroupById")
    public ResponseResult<Integer> deGroupById(String id,String ip){

        Integer data = androidService.deGroupById(id,ip);

        return new ResponseResult<>(SUCCESS,data);
    }




    /**
     * 查看素材根据分组id
     * @param ip
     * @param groupId
     * @return
     */
    @RequestMapping("getMaterialByGroupId")
    public ResponseResult<ArrayList<MaterialEVo> > getMaterialByGroupId(String ip,String groupId,String daoId){

        ArrayList<MaterialEVo> data  = androidService.getMaterialByGroupId(ip,groupId,daoId);


        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 所有素材
     * @param ip
     * @return
     */
    @RequestMapping("getMaterialAll")
    public ResponseResult<ArrayList<MaterialEVo> > getMaterialAll(String ip,String daoId){

        ArrayList<MaterialEVo> data  = androidService.getMaterialAll(ip,daoId);

        return new ResponseResult<>(SUCCESS,data);
    }

    //String ip, String materialName

    /**
     * 搜索
     * @param ip
     * @param materialName
     * @return
     */
    @RequestMapping("getMaterialByName")
    public ResponseResult<ArrayList<MaterialEVo> > getMaterialByName(String ip,String materialName,String daoId){

        ArrayList<MaterialEVo> data  = androidService.findMaterialByName(ip,materialName,daoId);

        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     *
     * @param files
     * @param materialName
     * @param groupId
     * @param imgDescribe
     * @param ip
     * @param price
     * @return
     */
    @RequestMapping("addMaterial")
    public ResponseResult<Integer> addMaterial(MultipartFile[] files, String materialName, String groupId, String imgDescribe,String ip,String price){
        System.err.println(materialName+ip);
        Integer data = androidService.insertMaterial(files,materialName,groupId,imgDescribe,ip,price);

        return new ResponseResult<>(SUCCESS,data);
    }

    /*************************************************买点Fab培训***************************************************************/

    /**
     * 获取FAB卖点培训根据款号
     * @param ip
     * @param code
     * @return
     */
    @RequestMapping("findDataByCode")
    public ResponseResult<FABVo> findDataByCode(String ip, String code) {
        FABVo data = androidService.findDataByCode(code, ip);
        return new ResponseResult<FABVo>(SUCCESS,data);
    }

    /**
     * 获取FAB卖点培训根据款号
     * @param ip
     * @param code
     * @return
     */
    @RequestMapping("findFabByCodeOrName")
    public ResponseResult<List<FABVo>> findFabByCodeOrName(String ip, String code) {
        List<FABVo> data = androidService.findFabByCodeOrName(ip, code);
        return new ResponseResult<>(SUCCESS, data);
    }




}
